package com.pet.serve.controller;

import com.pet.serve.entity.Schedule;
import com.pet.serve.response.ApiResponse;
import com.pet.serve.security.CustomUserDetails;
import com.pet.serve.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/schedule")
public class ScheduleController {

    private final ScheduleService scheduleService;

    @Autowired
    public ScheduleController(ScheduleService scheduleService) {
        this.scheduleService = scheduleService;
    }

    // 添加新的日程
    @PostMapping("/add")
    public ResponseEntity<Schedule> addSchedule(@RequestBody Schedule schedule) {
        Schedule savedSchedule = scheduleService.addSchedule(schedule);
        return ResponseEntity.status(201).body(savedSchedule);
    }

    // 更新日程
    @PutMapping("/update")
    public ResponseEntity<Schedule> updateSchedule(@RequestBody Schedule schedule) {
        Schedule updatedSchedule = scheduleService.updateSchedule(schedule);
        return ResponseEntity.ok(updatedSchedule);
    }

    // 根据ID删除日程
    @DeleteMapping("/delete/{id}")
    public ResponseEntity<Void> deleteSchedule(@PathVariable Long id) {
        scheduleService.deleteSchedule(id);
        return ResponseEntity.noContent().build();
    }

    // 根据用户名查找日程
    @GetMapping("/byUsername")
    public ResponseEntity<List<Schedule>> getSchedulesByUsername(@RequestParam String username) {
        List<Schedule> schedules = scheduleService.getSchedulesByUsername(username);
        return ResponseEntity.ok(schedules);
    }

    // 根据日期查找日程
    @GetMapping("/byDay")
    public ResponseEntity<List<Schedule>> getSchedulesByDay(@RequestParam Date day) {
        List<Schedule> schedules = scheduleService.getSchedulesByDay(day);
        return ResponseEntity.ok(schedules);
    }

    // 根据日期范围查找日程
    @GetMapping("/betweenDates")
    public ResponseEntity<List<Schedule>> getSchedulesBetweenDates(@RequestParam Date startDate, @RequestParam Date endDate) {
        List<Schedule> schedules = scheduleService.getSchedulesBetweenDates(startDate, endDate);
        return ResponseEntity.ok(schedules);
    }

    // 分页查找所有日程
    @GetMapping("/pagination")
    public ResponseEntity<List<Schedule>> getAllSchedules(@RequestParam int pageNo, @RequestParam int pageSize) {
        List<Schedule> schedules = scheduleService.getAllSchedules(pageNo, pageSize);
        return ResponseEntity.ok(schedules);
    }

    // 获取当前用户的日程安排
    @GetMapping("/get")
    public ResponseEntity<?> getSchedule() {
        // 假设我们从 SecurityContext 中获取当前登录的用户名
        String currentUsername = ((CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername();

        // 调用服务获取该用户的日程安排
        List<Schedule> schedules = scheduleService.getSchedulesByUsername(currentUsername);

        // 返回结果
        if (schedules.isEmpty()) {
            return ResponseEntity.ok(new ApiResponse(false, "No schedules found", null));
        }
        return ResponseEntity.ok(new ApiResponse(true, "Schedules fetched", schedules));
    }
}
